package com.zs4sdk.activity.login;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.common.library.data.CommonEventHandler;
import com.common.library.data.EventHandler2Get;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.NetworkHelper;
import com.common.library.util.ParamsHelper;
import com.zs4gamesdk.R;
import com.zs4sdk.common.Extra;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * Created by Administrator on 2018/4/17.
 */

public class Zs4GameLoginInputPhone extends Zs4GameLoginBase{
    private ViewStub stuInputPhone;
    private Button btnNext;
    private EditText etInputPhone;
    private TextView tvInputPhoneWarning;
    public static final int NO_REGIST = 2;//手机号属于未注册
    public static final int REGIST_NO_PASSWORD= 3;
    public static final int REGIST_SET_PASSOWRD = 4;
    private int verifyPhoneResult = 1;
    public Zs4GameLoginInputPhone(Activity act, View view, Handler handler) {
        super(act, view, handler);
        stuInputPhone = (ViewStub)mMainView.findViewById(R.id.stu_input_phone);
    }
    public Object getData(){
        String phoneNumber = null;
        if(etInputPhone != null){
            phoneNumber = etInputPhone.getText().toString().trim();
        }
        return phoneNumber;
    }
    @Override
    public void login() {
        String phoneNumber = null;
        if(etInputPhone != null){
            phoneNumber = etInputPhone.getText().toString().trim();
        }
        getoVerifyPhone(phoneNumber);
    }

    @Override
    protected void show() {
        stuInputPhone.setVisibility(View.VISIBLE);
        if(!mIsInit){
            mIsInit = true;
            etInputPhone = (EditText)mMainView.findViewById(R.id.zs4game_edt_input_login_phone);
            btnNext = (Button)mMainView.findViewById(R.id.btn_zs4game_next);
            if(mOnClickListener != null) {
                btnNext.setOnClickListener(mOnClickListener);
            }
            tvInputPhoneWarning =(TextView)mMainView.findViewById(R.id.tvInputPhoneWarning);
        }
    }

    @Override
    protected void hide() {
        stuInputPhone.setVisibility(View.GONE);
    }

    @Override
    public void doOnClick(View v) {

    }

    public boolean isCanDoNext(){
        String phoneNumber = null;
        if(etInputPhone != null){
            phoneNumber = etInputPhone.getText().toString().trim();
        }
        Log.e("ph"," phoneNumber is "+phoneNumber);
        if(phoneNumber == null || phoneNumber.length() == 0){
            tvInputPhoneWarning.setVisibility(View.VISIBLE);
            return false;
        }else{
            tvInputPhoneWarning.setVisibility(View.GONE);
        }
        return true;
    }

    private void getoVerifyPhone(String phoneNumber) {//验证手机号码是否有效
        if (NetworkHelper.isNetworkAvailable(mActivity)) {
            HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.SHOWPROCESS);
            ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
            paramList.add(new BasicNameValuePair("mobile", EncryptHelper.encryptByAES(phoneNumber)));
            new EventHandler2Get(
                    Extra.URL.VERIFY_PHONE,//      PHONEVERIFYCODE2,
                    paramList,
                    ParamsHelper.getUserAgent1(), new CommonEventHandler.ResultHandlerInEvent() {
                @Override
                public void doResultHanler(JSONObject result) throws JSONException {
                    Log.e("phone"," verify result is "+result.toString());
                    HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);
                    Message msg = mLoginHandler.obtainMessage();
                    msg.what = Extra.MessageKey.VERIFY_PHONE_REGIDTER_OR_LOGIN;
                    int res = result.getInt("result");
                    msg.arg1 = res;
                //  msg.obj = result;
                    Bundle bundle = new Bundle();
                    msg.setData(bundle);
                    // 发送消息
                    mLoginHandler.sendMessage(msg);
                }
                @Override
                public void doExceptionHandler() {
                    HandlerUtil.sendMessage(mLoginHandler, Extra.MessageKey.DISMISSPROCESS);
                }
            });
        } else {
            tvInputPhoneWarning.setVisibility(View.VISIBLE);
            tvInputPhoneWarning.setText(ActivityHelper.getStringResId("zs4gamestring_hint_has_not_network"));
            return;
        }
    }
}
